﻿using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
using Microsoft.SharePoint;

namespace SPQueryToSql.ApplicationObjects.CAML
{
    internal class Lt : BaseOperator
    {
        public Lt(XContainer element, IEnumerable<FieldInfo> listFields)
            : base(element, listFields)
        {
        }

        public override string CAMLElementName
        {
            get { return "Lt"; }
        }

        public override string ToSql(Query query)
        {
            var sql = new StringBuilder();
            switch (FieldInfo.Type)
            {
                case SPFieldType.Invalid:
                case SPFieldType.AllDayEvent:
                    break;
                case SPFieldType.Text:
                    sql.AppendFormat("[{0}] < '{1}'", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.Note:
                    sql.AppendFormat("[{0}] < '{1}'", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.DateTime:
                    sql.AppendFormat("[{0}] < '{1}'", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.Choice:
                    sql.AppendFormat("[{0}] < '{1}'", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.URL:
                    sql.AppendFormat("[{0}] < '{1}'", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.Integer:
                    sql.AppendFormat("[{0}] < {1}", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.Counter:
                    sql.AppendFormat("[{0}] < {1}", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.Number:
                    sql.AppendFormat("[{0}] < {1}", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.Currency:
                    sql.AppendFormat("[{0}] < {1}", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.MaxItems:
                    sql.AppendFormat("[{0}] < {1}", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.Computed:
                    sql.AppendFormat("[{0}] < '{1}'", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.Calculated:
                    sql.AppendFormat("[{0}] < '{1}'", FieldInfo.InternalName, Value.Text);
                    break;
                default:
                    sql.Append("1=1");
                    break;
            }
            return sql.ToString();
        }
    }
}
